<?php


namespace app\model;


use fast\Http;
use Ixudra\Curl\Facades\Curl;
use think\facade\Cache;
use think\facade\Log;
use think\Model;

class MqttUserManage extends Model {



    /**
     * 注册回调省厅，并注册洞鉴
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function callback($id,$photo=''){

        $service_type=config('capture.platform');
        $service=null;
        eval('$service= new app\service\impl\Albums'.$service_type.'ServiceImpl();');
        $data=MqttUserManage::find($id);
        $old_photo=base64_img($data->photo,0);
        if(!$photo){
            $photo=base64_img($data->photo);
        }
        $data->photo=$photo;
        //发送洞鉴
        // $post_ai_return=$service->create_face($data);
        // $post_ai_return_array=json_decode($post_ai_return,true);
        // $photo_id=0;
        // if($post_ai_return_array['code']===0){
        //     $photo_id=$post_ai_return_array['data']['photo_id'];
        // }
        // MqttUserManage::where('id',$id)->update(['status'=>'1','local_json'=>$post_ai_return,'photo_id'=>$photo_id]);

        
        // 不使用洞鉴
         MqttUserManage::where('id',$id)->update(['status'=>'1']);
        if($data->callback_status==1){
            return;
        }
        //回调
        $post_json=array(
            'id'=>$data->ywlsh+0,
            'statusName'=>'审核通过',
            'statusCode'=>'02',
            'communityCode'=>$data->address_id,
            'period'=>'always',
        );
        /*$post_json[]=[
            'ywlsh'=>$data->ywlsh+0,
            'result'=>'1',
            'period'=>'always',
        ];*/
        $areaname=$data['address_id'];
        $_aizone=new Aizone();
        $access_value=$_aizone->get_token($areaname);

        $posturl='https://ezc.chinapopin.com/zhxq-resource/user/userCallback';

        $login_header=array(
            CURLOPT_HTTPHEADER=>['Content-Type:application/json;charset=UTF-8','Authorization:bearer '.$access_value],
        );


        $post_ai_return = Http::post($posturl,json_encode($post_json),$login_header);
        Log::info('返回省厅header：'.json_encode($login_header));
        Log::info('返回省厅：'.json_encode($post_json));
        Log::info('返回省厅结果：'.$post_ai_return);
        sleep(1);
        $res=json_decode($post_ai_return,true);
        if(!$res){
            $access_value=$_aizone->get_token($areaname,1);
            $login_header=array(
                CURLOPT_HTTPHEADER=>['Content-Type:application/json;charset=UTF-8','Authorization:bearer '.$access_value],
            );
            $post_ai_return = Http::post($posturl,json_encode($post_json),$login_header);
            Log::info('二次返回省厅header：'.json_encode($login_header));
            Log::info('二次返回省厅：'.json_encode($post_json));
            Log::info('二次返回省厅结果：'.$post_ai_return);
            $res=json_decode($post_ai_return,true);
        }
        if($res&&$res['code']==20000){
            $status=1;
        }else{
            $status=0;
            $post_ai_return='{}';
        }
        MqttUserManage::where('id',$id)->update(['out_json'=>$post_ai_return,'callback_status'=>$status,'callback_time'=>date('Y-m-d H:i:s')]);
        //向面板机发送数据
        // Log::info("向面板机发送数据");
        // $_panel=new \app\model\Panel();
        // $_panel->add_people($areaname,$data->id,$data['name'],$data->ywlsh,$old_photo);

    }

}